---
import { getCollection, render } from "astro:content";

import Layout from "../../../layouts/Layout.astro";
import MarkdownLayout from "../../../layouts/MarkdownLayout.astro";

export async function getStaticPaths() {
  const blogEntries = await getCollection("posts");

  const sortedEntries = blogEntries.sort(
    (a, b) =>
      new Date(b.data.pubDate).getTime() - new Date(a.data.pubDate).getTime(),
  );

  // Map through the sorted entries to assign prevEntry and nextEntry
  return sortedEntries.map((entry, index) => {
    const prevEntry = sortedEntries[index - 1] || null;
    const nextEntry = sortedEntries[index + 1] || null;

    return {
      params: { slug: entry.id },
      props: {
        entry,
        prevEntry,
        nextEntry,
        slug: entry.id,
      },
    };
  });
}

const { entry, prevEntry, nextEntry } = Astro.props;
const { Content } = await render(entry);
const pageTitle = `NativeLink News | ${entry.id}`;

export const prerender = true;
---

<Layout title={pageTitle}>
    <MarkdownLayout frontmatter={entry.data} prevEntry={prevEntry} nextEntry={nextEntry} slug={entry.id}>
        <Content />
    </MarkdownLayout>
</Layout>
